<template>
  <div class="login-wrapper">
    <div class="login-container">
      <div class="logo-container">
        <div class="logo-box">
          <img class="logo" src="@/assets/logo.png" />
        </div>
        <div class="text">Vue3后台管理系统beta</div>
      </div>
      <el-form class="login-form" ref="formRef" :model="form" :rules="rules">
        <el-form-item class="form-item" prop="username">
          <icon class="icon" name="User" size="20"></icon>
          <el-input v-model="form.username" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item class="form-item" prop="password">
          <icon class="icon" name="Lock" size="20"></icon>
          <el-input v-model="form.password" type="password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary" size="large" @click="login(formRef)" v-loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup>
import { useUserStore } from '@/store'
import { useRouter } from 'vue-router'
import userMock from '@/mock/user'
const userStore = useUserStore()
const router = useRouter()
const loading = ref(false)
const formRef = ref()
const rules = reactive({
  username: [
    { required: true, message: '账号不能为空', trigger: 'change' },
    { min: 3, max: 13, message: '请输入3~13位账号', trigger: 'change' },
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'change' },
    { min: 6, max: 13, message: '请输入6~13位密码', trigger: 'change' },
  ],
})

const form = reactive({
  username: 'admin',
  password: '123456',
})
function login(formRef) {
  loading.value = true
  formRef.validate((valid, fields) => {
    if (valid) {
      userMock
        .login(form.username, form.password)
        .then((res) => {
          userStore.data = res.data
          userStore.roles = res.data.roles
          userStore.auths = res.data.auths
          router.push('/')
        })
        .finally(() => {
          loading.value = false
        })
    } else {
      setTimeout(function () {
        loading.value = false
      }, 1000)
    }
  })
}

function keydown(e) {
  if (e.key === 'Enter') {
    document.querySelector('.login-btn').click()
  }
}
onMounted(() => {
  document.addEventListener('keydown', keydown)
})
onUnmounted(() => {
  document.removeEventListener('keydown', keydown)
})
</script>

<style lang="scss" scoped>
.login-wrapper {
  width: 100%;
  height: 100%;
  background: url('@/assets/bg.png');
  background-size: cover;
  overflow: hidden;
  .login-container {
    height: 500px;
    width: 450px;
    margin: 10% 10% 0 0;
    float: right;
    background: #fff;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
    border-radius: 8px;
    .logo-container {
      width: 400px;
      text-align: center;
      .logo-box {
        height: 120px;
        margin-bottom: 20px;
        .logo {
          height: 100%;
        }
      }
      .text {
        font-size: 36px;
        font-weight: 600;
        color: #000;
        margin-bottom: 10px;
      }
    }
    .login-form {
      width: 280px;
      margin: 0 auto;
      .form-item {
        :deep(input) {
          padding-left: 20px;
          height: 40px;
        }
        .icon {
          position: absolute;
          left: 7px;
          z-index: 1;
          color: $color-1;
        }
      }
      .login-btn {
        width: 100%;
        margin-top: 10px;
        overflow: hidden;
      }
    }
  }
}
</style>
